package com.egao.orderB.service.impl;

import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
//import com.egao.orderB.mapper.PutGoodsMapper;
import com.egao.orderB.biz.PutStorageBiz;
import com.egao.orderB.biz.StockBiz;
import com.egao.orderB.entity.ExStorage;
import com.egao.orderB.entity.PutGoods;
import com.egao.orderB.entity.PutStorage;
import com.egao.orderB.entity.Stock;
import com.egao.orderB.service.PutGoodsService;
import org.springframework.stereotype.Service;
import com.egao.orderB.biz.PutGoodsBiz;
import net.sf.json.JSONObject;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
//import javax.transaction.Transactional;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 入库商品详情服务实现类
 * @author : Doman
 * @date : 2021-12-23 15:04:33
 * @version ：1.0
 */
@Service
public class PutGoodsServiceImpl /*extends ServiceImpl<PutGoodsMapper, PutGoods> */implements PutGoodsService {

	@Autowired
	private PutStorageBiz putStorageBiz;
	@Autowired
	private PutGoodsBiz putGoodsBiz;
	@Autowired
	private StockBiz stockBiz;

  @Override
  public JsonResult list(PageResult2<T> page, PutGoods putGoods, User loginUser)  throws Exception{
    return putGoodsBiz.list(page,putGoods,loginUser);
  }

  @Override
  public JsonResult list(PutGoods putGoods, User loginUser) {
    return JsonResult.ok().setData(putGoodsBiz.list(putGoods,loginUser));
  }

  @Override
  public JsonResult getById(PutGoods putGoods) {
    return JsonResult.ok().setData(putGoodsBiz.getById(putGoods));
  }

  @Transactional
  @Override
  public JsonResult save(PutGoods putGoods, User loginUser) {
    if (putGoodsBiz.save(putGoods, loginUser)) {
      return JsonResult.ok("添加成功");
    }
    return JsonResult.error("添加失败");
  }

  @Transactional
  @Override
  public JsonResult update(PutGoods putGoods, User loginUser) {
    PutGoods putGoodsOld = null;
    try {
      putGoodsOld = putGoodsBiz.getById(putGoods);
    } catch (Exception e) {
      e.printStackTrace();
    }
    float num = putGoods.getNum() - putGoodsOld.getNum();
    float total_weight = putGoods.getTotalWeight() - putGoodsOld.getTotalWeight();
    float total_volume = putGoods.getTotalVolume() - putGoodsOld.getTotalVolume();

    PutStorage putStorage = new PutStorage();
    putStorage.setId(putGoodsOld.getPutInStorageId()+"");
    putStorage.setTotalGrossWeight(total_weight+"");
    putStorage.setVolumeM3(total_volume+"");
    putStorage.setNum(num);
    putStorageBiz.updateNum(putStorage, loginUser);


    if (putGoodsBiz.updateNum(putGoods, loginUser)) {
      Stock stock = new Stock();
      stock.setCustomerId(putGoodsOld.getCustomerId());
      stock.setSku(putGoodsOld.getSku());
      stock.setName(putGoodsOld.getName());
      stock.setPack(putGoodsOld.getPack());
      stock.setLength(putGoodsOld.getLength());
      stock.setWidth(putGoodsOld.getWidth());
      stock.setHigh(putGoodsOld.getHigh());
      List<Stock> stockListUnique = stockBiz.listUnique(stock);
      stock = stockListUnique.get(0);
      stock.setNum(num );
      stockBiz.updateNum(stock, loginUser);
      return JsonResult.ok("修改成功");
    }
    return JsonResult.error("修改失败");
  }

  @Transactional
  @Override
  public JsonResult remove(PutGoods putGoods, User loginUser) {
    PutGoods putGoodsOld = putGoodsBiz.getById(putGoods);
    if (putGoodsBiz.remove(putGoods, loginUser)) {

      PutStorage putStorage = new PutStorage();
      putStorage.setId(putGoodsOld.getPutInStorageId()+"");
      putStorage.setNum(0 - putGoodsOld.getNum());;
      putStorage.setTotalGrossWeight((0-putGoodsOld.getTotalWeight())+"");
      putStorage.setVolumeM3(0-putGoodsOld.getTotalVolume()+"");
      putStorageBiz.updateNum(putStorage, loginUser);

      Stock stock = new Stock();
      stock.setCustomerId(putGoodsOld.getCustomerId());
      stock.setSku(putGoodsOld.getSku());
      stock.setName(putGoodsOld.getName());
      stock.setPack(putGoodsOld.getPack());
      stock.setWeight(putGoodsOld.getWeight());
      stock.setLength(putGoodsOld.getLength());
      stock.setWidth(putGoodsOld.getWidth());
      stock.setHigh(putGoodsOld.getHigh());
      List<Stock> stockListUnique = stockBiz.listUnique(stock);
      stock = stockListUnique.get(0);
      stock.setNum(0 - putGoodsOld.getNum());
      stockBiz.updateNum(stock, loginUser);
      return JsonResult.ok("删除成功");
    }
    return JsonResult.error("删除失败");
  }

}
